package com.bootssm.controller;

import com.bootssm.pojo.*;
import com.bootssm.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/borrow")
public class BorrowController {
    @Autowired
    private BorrowService borrowService;

    @PostMapping("/findAll")
    public PageView<Borrow> findAll(@RequestBody BorrowVO borrowVO) {
        Integer total = borrowService.getTotal(borrowVO);
        List<Borrow> borrowList = borrowService.findAll(borrowVO);
        PageView<Borrow> pageView=new PageView<>();
        pageView.setDataList(borrowList);
        pageView.setCount(total);
        return pageView;
    }

    @PutMapping("update")
    public Result update(@RequestBody Borrow borrow) {
        try{
            borrowService.update(borrow);
        }catch (Exception e){
            e.printStackTrace();
            return new Result("500","审核不通过");
        }
        return new Result("200","审核通过");
    }

    @PostMapping("insert")
    public Result insert(@RequestBody Borrow borrow) {
        try{
            borrowService.insert(borrow);
        }catch (Exception e){
            e.printStackTrace();
            return new Result("500","申请失败");
        }
        return new Result("200","申请成功");
    }

    @PostMapping("/delete")
    public Result deleteById(@RequestBody Borrow borrow) {
        try{
            borrowService.deleteById(borrow);
        }catch (Exception e){
            e.printStackTrace();
            return new Result("500","取消申请失败");
        }
        return new Result("200","取消申请成功");
    }
}
